人工智能多模态方向学习笔记Attention and Tell

您所在的位置:网站首页 人工智能 attention 人工智能多模态方向学习笔记Attention and Tell

人工智能多模态方向学习笔记Attention and Tell

2023-03-24 02:55| 来源: 网络整理| 查看: 265

简介

Attention and Tell(通常缩写为“Attn-Tell”)是一种机器学习算法,用于自然语言处理任务,如文本摘要和问答。

该算法由两个主要组件组成:注意力机制和解码器。注意力机制用于识别输入序列的重要部分,而解码器根据注意力机制提供的信息生成输出序列。

注意机制的工作原理是根据每个输入令牌与当前生成的输出令牌的相关性为其分配权重。这些权重是使用当前输出令牌和每个输入令牌之间的点积计算的,该权重生成一个权重向量,该向量经过归一化以表示概率分布。然后使用此概率分布对输入令牌进行加权,以便最相关的令牌具有最高的权重。

解码器使用加权输入标记生成输出序列,一次一个标记。在每个时间步长,解码器使用以前的输出令牌和加权输入令牌在可能的输出令牌上生成概率分布。然后选择最可能的标记作为下一个输出标记,并重复该过程,直到生成所需的输出序列。

总体而言,Attn-Tell 算法是自然语言处理任务的强大工具,这些任务需要生成准确且信息丰富的摘要或答案。它能够识别重要信息并产生高质量的输出,使其成为各种应用的热门选择。

原理推导

注意和告诉算法可以从编码器-解码器架构派生,并增加了注意力机制。

编码器-解码器架构: 编码器-解码器架构用于机器翻译和摘要等任务。输入序列首先被编码为固定长度的矢量表示,解码器基于该表示生成输出序列。编码器和解码器通常使用递归神经网络(如 LSTM)实现。

注意力机制: 注意力机制被添加到编码器-解码器架构中,以允许解码器在每个时间步长专注于输入序列的不同部分。注意机制的工作原理是根据每个输入令牌与正在生成的当前输出令牌的相关性为其分配权重。这些权重是使用当前输出令牌和每个输入令牌之间的点积计算的,该权重生成一个权重向量,该向量经过归一化以表示概率分布。然后使用此概率分布对输入令牌进行加权,以便最相关的令牌具有最高的权重。

注意和告诉算法: Attention and Tell 算法通过允许解码器一次生成一个标记的输出序列来扩展注意力机制,使用注意力机制在每个时间步长关注输入序列中最相关的部分。

编码器:输入序列首先使用编码器网络进行编码,从而生成一系列隐藏状态。每个隐藏状态表示输入序列的不同部分。

解码器:解码器网络一次生成一个令牌的输出序列。在每个时间步长,解码器将以前的输出令牌和以前的隐藏状态作为输入,并生成新的隐藏状态。

注意: 注意机制应用于编码器产生的隐藏状态和解码器产生的当前隐藏状态,以计算编码器隐藏状态的加权和。生成的上下文向量表示当前时间步长的输入序列中最相关的部分。

输出:解码器的上下文向量和当前隐藏状态连接并通过线性层传递,以在可能的输出令牌上生成概率分布。选择最有可能的令牌作为下一个输出令牌。

重复:重复该过程,直到生成所需的输出序列。

总体而言,Attention and Tell 算法是自然语言处理任务的强大工具,这些任务需要生成准确且信息丰富的摘要或答案。它能够识别重要信息并产生高质量的输出,使其成为各种应用的热门选择。

伪代码 # Encoder network encoder_outputs = [] for input_token in input_sequence: encoder_output, encoder_hidden = encoder_rnn(input_token, encoder_hidden) encoder_outputs.append(encoder_output) # Decoder network with attention decoder_hidden = decoder_rnn.initialize_hidden_state() decoder_input = start_token output_sequence = [] for i in range(output_length): decoder_output, decoder_hidden = decoder_rnn(decoder_input, decoder_hidden) attention_weights = calculate_attention_weights(encoder_outputs, decoder_hidden) context_vector = torch.matmul(attention_weights, encoder_outputs) output = output_layer(torch.cat([context_vector, decoder_output], dim=1)) output_token = choose_output_token(output) output_sequence.append(output_token) decoder_input = output_token # Attention mechanism def calculate_attention_weights(encoder_outputs, decoder_hidden): attention_scores = [] for encoder_output in encoder_outputs: score = calculate_attention_score(encoder_output, decoder_hidden) attention_scores.append(score) attention_weights = F.softmax(torch.cat(attention_scores), dim=0) return attention_weights def calculate_attention_score(encoder_output, decoder_hidden): energy = decoder_attention(encoder_output) + decoder_hidden score = decoder_attention_activation(energy) return score # Output selection def choose_output_token(output): output_probs = F.softmax(output, dim=1) output_token = torch.argmax(output_probs, dim=1) return output_token


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3